package controller;

import helper.UserSession;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import databeans.Customer;
import databeans.Employee;


import model.CustomerDAO;
import model.Model;


public class IndexAction extends Action {
    private CustomerDAO customerDAO;

    public IndexAction(Model model) {
        customerDAO = model.getCustomerDAO();
    }

    public String getName() {
        return "index.do";
    }

    public String perform(HttpServletRequest request) {
        //ATTENTION
        // define target page
    	String customerloginPage = "customer/customer-login.jsp";
    	String employeeloginPage = "employee/employee-login.jsp";
        String customerdashboardPage = "customerDashboard.do";
        String employeedashboardPage = "employeeDashboard.do";
        
        //set the employee
        Employee employee = null;
        Customer customer = null;

        
        //errors and messages will be stored here
        //resetting those containers
        List<String> errors = new ArrayList<String>();
        List<String> messages = new ArrayList<String>();
   
        //define the error and message
        request.setAttribute("errors", errors);
        request.setAttribute("messages", messages);
        
        // Define user type for this controller
        request.setAttribute("page", "login");
        HttpSession session = request.getSession(true); 
        
        //check if employee has been logged in
        employee = (Employee) request.getSession(true).getAttribute("employee");
        if (employee != null) {
        	request.setAttribute("page", "emp-home");
        	session.setAttribute("employee", employee);
            return employeedashboardPage ;
        }
        
        //check if customer has actually logged in
        customer = (Customer) request.getSession(true).getAttribute("customer");
        if (customer != null) {
            request.setAttribute("page", "cust-home");
            session.setAttribute("customer", customer);
            return customerdashboardPage ;
        }
        
        request = UserSession.removeEmployeeFromSession(request);
        request = UserSession.removeCustomerFromSession(request);
        
        return "IndexPage.jsp";
    }
}